﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using rsdeploygui.ReportService2010;

namespace rsdeploygui
{
    class LinkedReport
    {
        private ReportingService2010 rs;

        public LinkedReport (string url)
        {
            try
            {
                rs = new ReportingService2010();
                rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
                rs.Url = url;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// Does the linked report have a valid parent report?
        /// </summary>
        /// <param name="ItemPath"></param>
        /// <returns></returns>
        public Boolean HasValidReportLink(string ItemPath)
        {
            Boolean output = false;
            try
            {
                Property rProp = new Property();
                rProp.Name = "HasValidReportLink";
                Property[] props = new Property[1];
                props[0] = rProp;

                Property[] properties = this.rs.GetProperties(ItemPath, props);
                foreach (Property prop in properties)
                {
                    if (prop.Value == "True")
                    {
                        output = true;
                    }
                }
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }            
            return output;
        }

        /// <summary>
        /// Get the path to the item that is used as the parent item for the linked report.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string GetItemLink(string path)
        {
            string output = "";
            try
            {
                output = this.rs.GetItemLink(path);
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }
            return output;
        }

        /// <summary>
        /// Refresh the linked item to the existing linked ParentItem
        /// </summary>
        /// <param name="ParentPath"></param>
        /// <returns></returns>
        public Boolean RefreshLinkedItem(string ItemPath, string ParentPath)
        {
            Boolean output = false;
            try
            {
                // debugging
                Property[] properties = this.rs.GetProperties(ItemPath, null);
                foreach (Property prop in properties)
                {
                    Console.WriteLine("{0} : {1}", prop.Name, prop.Value);
                }
                // set item link
                //Console.WriteLine("this.rs.SetItemLink({0}, {1})", ItemPath, this.rs.GetItemLink(ItemPath));
                
                //this.rs.SetItemLink(ItemPath, ParentPath);
                output = true;
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }
            return output;
        }
    }
}
